package find.prj.prog;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import find.prj.lib.SortedMegaList;
import find.prj.lib.TurboList;
import find.prj.lib.TurboNode;
import find.prj.lib.TwoList;

public class ListTesterConsole {

	public static void main(String[] args) {
		File file = new File("C:\\Users\\Alek\\workspace\\mega-test-project\\FindProject\\1.txt");
		long startTime, middleTime, finishTime;
		
		
		startTime = System.currentTimeMillis();
		ArrayList<Integer> arrayList = new ArrayList<Integer>();
		fileNumbersToArray (file, arrayList);
		middleTime = System.currentTimeMillis();
		System.out.println("File loaded: "+(middleTime-startTime));
		ArrayListSort(arrayList, false);
		finishTime = System.currentTimeMillis();
		System.out.println("Sorted: "+(finishTime-startTime));
		ListToFile(arrayList, "C:\\Users\\Alek\\workspace\\mega-test-project\\FindProject\\arraylist.txt");

		startTime = System.currentTimeMillis();
		TurboList<Integer> turboList = new TurboList<Integer>();
		fileNumbersToArray (file, turboList);
		middleTime = System.currentTimeMillis();
		System.out.println("File loaded: "+(middleTime-startTime));
		TurboListSort(turboList, false);
		finishTime = System.currentTimeMillis();
		System.out.println("Sorted: "+(finishTime-startTime));
		ListToFile(arrayList, "C:\\Users\\Alek\\workspace\\mega-test-project\\FindProject\\turbolist.txt");

		startTime = System.currentTimeMillis();
		ArrayList<Integer> arrayList2 = new ArrayList<Integer>();
		fileNumbersToArray (file, arrayList2);
		int[] array = new int[arrayList.size()];
		for (int i=0; i < array.length; i++)
			array[i] = arrayList.get(i);
		middleTime = System.currentTimeMillis();
		System.out.println("File loaded: "+(middleTime-startTime));
		ArraySort(array, false);
		finishTime = System.currentTimeMillis();
		System.out.println("Sorted: "+(finishTime-startTime));
		arrayList2.clear();
		for (Integer value : array)
			arrayList2.add(value);
		ListToFile(arrayList2, "C:\\Users\\Alek\\workspace\\mega-test-project\\FindProject\\array.txt");
		
		startTime = System.currentTimeMillis();
		SortedMegaList sortedMegaList = new SortedMegaList();
		fileNumbersToArray (file, sortedMegaList);
		middleTime = System.currentTimeMillis();
		System.out.println("File loaded: "+(middleTime-startTime));
		finishTime = System.currentTimeMillis();
		System.out.println("Sorted: "+(finishTime-startTime));
		ListToFile(arrayList, "C:\\Users\\Alek\\workspace\\mega-test-project\\FindProject\\sortedmegalist.txt");	
	}

	public static void fileNumbersToArray (File file, Collection<Integer> list) {
		BufferedReader reader = null;

		try {
		    reader = new BufferedReader(new FileReader(file));
		    String numbersString = null;

		    while ((numbersString = reader.readLine()) != null) {
		        list.add(new Integer(numbersString));
		    }
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null)
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
	}
	
	public static void ArrayListSort (ArrayList<Integer> list, boolean shortMethod)
	{
		Integer minVal=0, kVal = 0, kPlusVal=0;
		Integer minI=0;
		if (shortMethod) {
			for (int i=0; i < list.size(); i++) {
				for (int k=0; k < list.size()-i-1 ; k++) {
					kVal = list.get(k);
					kPlusVal = list.get(k+1);
					if (kVal > kPlusVal) {
						list.set(k, kPlusVal);
						list.set(k+1, kVal);
					}
				}
			}
		}
		else {
			for (int i=0; i < list.size(); i++) {
				minVal = list.get(i);
				minI = i; 
				for (int k=i+1; k < list.size(); k++) {
					kVal = list.get(k);
					if (kVal < minVal) {
						minVal = kVal;
						minI = k;
					}
				}
				kVal = list.get(i);
				list.set(i, minVal);
				list.set(minI,kVal);
			}
		}
	}

	public static void TurboListSort (TurboList<Integer> list, boolean shortMethod)
	{
		Integer minVal=0, kVal = 0, kPlusVal=0;
		Integer iterVal = 0 ;
		if (shortMethod) {
			for (int i=0; i < list.size(); i++) {
				for (int k=0; k < list.size()-i-1 ; k++) {
					kVal = list.get(k);
					kPlusVal = list.get(k+1);
					if (kVal > kPlusVal) {
						list.set(k, kPlusVal);
						list.set(k+1, kVal);
					}
				}
			}
		}
		else {
			TurboNode<Integer> iterNode = (TurboNode<Integer>) list.FirstNode;
			TurboNode<Integer> checkNode, minNode;
			while (iterNode.NextNode != null) {
				minVal = iterVal = iterNode.getValue();
				checkNode = minNode = iterNode;
				while (checkNode.NextNode != null) {
					checkNode = (TurboNode<Integer>) checkNode.NextNode;
					kVal = checkNode.getValue();
					if (kVal < minVal) {
						minVal = kVal;
						minNode = checkNode;
					}
				}
				iterNode.setValue(minVal);
				minNode.setValue(iterVal);
				iterNode = (TurboNode<Integer>) iterNode.NextNode;
			}
		}
	}
	
	public static void TwoListSort (TwoList<Integer> list, boolean shortMethod)
	{
		Integer minVal=0, kVal = 0, kPlusVal=0;
		Integer minI=0;
		if (shortMethod) {
			for (int i=0; i < list.size(); i++) {
				for (int k=0; k < list.size()-i-1 ; k++) {
					kVal = list.get(k);
					kPlusVal = list.get(k+1);
					if (kVal > kPlusVal) {
						list.set(k, kPlusVal);
						list.set(k+1, kVal);
					}
				}
			}
		}
		else {
			for (int i=0; i < list.size(); i++) {
				minVal = list.get(i);
				minI = i; 
				for (int k=i+1; k < list.size(); k++) {
					kVal = list.get(k);
					if (kVal < minVal) {
						minVal = kVal;
						minI = k;
					}
				}
				kVal = list.get(i);
				list.set(i, minVal);
				list.set(minI,kVal);
			}
		}
	}
	
	public static void ArraySort (int[] list, boolean shortMethod)
	{
		int minVal=0, kVal = 0, kPlusVal=0;
		int minI=0;
		if (shortMethod) {
			for (int i=0; i < list.length; i++) {
				for (int k=0; k < list.length-i-1 ; k++) {
					kVal = list[k];
					kPlusVal = list[k+1];
					if (kVal > kPlusVal) {
						list[k] = kPlusVal;
						list[k+1] = kVal;
					}
				}
			}
		}
		else {
			for (int i=0; i < list.length; i++) {
				minVal = list[i];
				minI = i; 
				for (int k=i+1; k < list.length; k++) {
					kVal = list[k];
					if (kVal < minVal) {
						minVal = kVal;
						minI = k;
					}
				}
				kVal = list[i];
				list[i] = minVal;
				list[minI] = kVal;
			}
		}
	}

	public static void ListToFile (Iterable<Integer> list, String fileName) {
		BufferedWriter writer = null;
		try
		{
			writer = new BufferedWriter( new FileWriter(fileName));
			for (Integer val : list)
				writer.write(val.toString()+"\r\n");
		}
		catch ( IOException exception)
		{
		}
		finally
		{
			try
			{
				if ( writer != null)
					writer.close( );
			}
			catch ( IOException exception)
			{
			}
		}
	}
	
	

}